﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using shopsoft.models.ClassBase;

namespace shopsoft.main.UserControls.PageControl
{
    [DefaultEvent("PageChanged"), ToolboxItem(typeof(PictureBox))]
    public partial class UCPager : UserControl
    {
        public delegate void PageChangedEventHandler(shopsoft.main.UserControls.PageControl.PageChangedEventArgs e);
        public event PageChangedEventHandler PageChanged;

        #region Private Property

        private DataTable dataT = null;
        private List<DataRecord> dataL = null;

        public List<DataRecord> dataList
        {
            get { return dataL; }
            set { dataL = value; }
        }

        private shopsoft.main.UserControls.PageControl.PageSourceType pageSourceType;
        private string pageInfo = "共 {0} 条数据，每页 {1} 条，共 {2} 页";
        private int pageSize = 10;
        
        #endregion Private Property

        #region Public Property

        /// <summary>
        /// 记录总数
        /// </summary>
        public int RecordCount { get; private set; }
        /// <summary>
        /// 每页数据数（默认10条）
        /// </summary>
        [DefaultValue(10), Category("Custom"), Description("每页数据数")]
        public int PageSize { get { return pageSize; } set { pageSize = value; pageSizeTextBox.Text = value.ToString(); } }
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount { get; private set; }
        /// <summary>
        /// 页码
        /// </summary>
        public int PageNumber { get; private set; }

        #endregion Public Property

        public UCPager()
        {
            InitializeComponent();
        }

        private void DefaultSetting()
        {
            lblPageInfo.Text = string.Format(pageInfo, 0, 0, 1);
            cmbCurrentPageNumber.Items.Add(1);
            cmbCurrentPageNumber.SelectedIndexChanged -= cmbCurrentPageNumber_SelectedIndexChanged;
            cmbCurrentPageNumber.SelectedIndex = 0;
            cmbCurrentPageNumber.SelectedIndexChanged += cmbCurrentPageNumber_SelectedIndexChanged;
        }

        /// <summary>
        /// 设置分页数据源
        /// </summary>
        /// <param name="dataTable">DataTable数据源</param>
        public void SetDataSource(DataTable dataTable)
        {
            dataT = dataTable;
            RecordCount = dataTable.Rows.Count;
            pageSourceType = shopsoft.main.UserControls.PageControl.PageSourceType.DataTable;
        }

        /// <summary>
        /// 设置分页数据源
        /// </summary>
        /// <param name="list">集合</param>
        public void SetDataSource(List<DataRecord> list)
        {
            dataL = list;
            RecordCount = list.Count;
            pageSourceType = shopsoft.main.UserControls.PageControl.PageSourceType.List;
        }


        /// <summary>
        /// 显示数据
        /// </summary>
        public void ShowData()
        {
            if (RecordCount == 0)
            {
                PageCount = 1;
            }
            else
            {
                PageCount = (int)Math.Ceiling(RecordCount * 1.0 / PageSize);
                btnFirstPage.Enabled = false;
                btnPreviousPage.Enabled = false;
            }

            lblPageInfo.Text = string.Format(pageInfo, RecordCount, PageSize, PageCount);

            cmbCurrentPageNumber.Items.Clear();
            for (int i = 1; i <= PageCount; i++)
            {
                cmbCurrentPageNumber.Items.Add(i);
            }
            if (cmbCurrentPageNumber.SelectedIndex == 0)
            {
                PageNumberChanged(1);
            }
            else
            {
                cmbCurrentPageNumber.SelectedIndex = 0;
            }
        }

        #region Event

        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            PageNumber = 1;
            cmbCurrentPageNumber.SelectedIndex = PageNumber - 1;
        }

        private void btnPreviousPage_Click(object sender, EventArgs e)
        {
            PageNumber--;
            cmbCurrentPageNumber.SelectedIndex = PageNumber - 1;
        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            PageNumber++;
            cmbCurrentPageNumber.SelectedIndex = PageNumber - 1;
        }

        private void btnLastPage_Click(object sender, EventArgs e)
        {
            PageNumber = PageCount;
            cmbCurrentPageNumber.SelectedIndex = PageNumber - 1;
        }

        private void cmbCurrentPageNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageNumberChanged(cmbCurrentPageNumber.SelectedIndex + 1);
        }

        #endregion Event

        #region Private Method

        /// <summary>
        /// 根据选择的页码绑定数据
        /// </summary>
        /// <param name="changePageNumber">页码</param>
        private void PageNumberChanged(int changePageNumber)
        {
            PageNumber = changePageNumber;
            CheckPageNumber(PageNumber);
            List<DataRecord> PageDataL = null;
            DataTable PageDataT = null;
            switch (pageSourceType)
            {
                case shopsoft.main.UserControls.PageControl.PageSourceType.DataTable:
                    {
                        PageDataT = dataT.AsEnumerable().Skip(PageSize * (PageNumber - 1)).Take(PageSize).CopyToDataTable();
                        break;
                    }
                case shopsoft.main.UserControls.PageControl.PageSourceType.List:
                    {
                        PageDataL = dataL.AsEnumerable().Skip(PageSize * (PageNumber - 1)).Take(PageSize).ToList();
                        break;
                    }
                default:
                    {
                        throw new Exception("未提供数据源");
                    }
            }
            shopsoft.main.UserControls.PageControl.PageChangedEventArgs e = new shopsoft.main.UserControls.PageControl.PageChangedEventArgs(PageDataL, PageDataT);
            if (PageChanged != null)
                PageChanged(e);
        }

        /// <summary>
        /// 检查当前页号 控制控件显示
        /// </summary>
        /// <param name="pageNumber">页码</param>
        private void CheckPageNumber(int pageNumber)
        {
            if (PageCount == 1)
            {
                btnFirstPage.Enabled = false;
                btnPreviousPage.Enabled = false;
                btnNextPage.Enabled = false;
                btnLastPage.Enabled = false;
            }
            else if (pageNumber == 1)
            {
                btnFirstPage.Enabled = false;
                btnPreviousPage.Enabled = false;
                btnNextPage.Enabled = true;
                btnLastPage.Enabled = true;
            }
            else if (pageNumber == PageCount)
            {
                btnFirstPage.Enabled = true;
                btnPreviousPage.Enabled = true;
                btnNextPage.Enabled = false;
                btnLastPage.Enabled = false;
            }
            else
            {
                btnFirstPage.Enabled = true;
                btnPreviousPage.Enabled = true;
                btnNextPage.Enabled = true;
                btnLastPage.Enabled = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pageSizeTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                int pageSize;
                if (int.TryParse(pageSizeTextBox.Text, out pageSize) && pageSize > 0)
                {
                    PageSize = pageSize;
                    ShowData();
                }
                else
                {
                    MessageBox.Show("请输入大于零的整数数字", "提示");
                    //Elike.Toolkit.ControlsErgodic.Windows.MsgBox.Information("提示", "请输入大于零的整数数字");
                }
            }
        }
        #endregion Private Method
    }
}
